package yxy.game.pm2.active.command.ccjj;

import yxy.cherry.data.bean.actgradefund;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_成长基金;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActCzjjInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Map;

@MessageType(proto = Command.ActCzjjInfo, classtype = ActiveMessage.ActCzjjInfoRequest.class)
public class CMD_Active_成长基金_Info extends Command<ActCzjjInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, ActCzjjInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActCzjjInfoResponse.Builder response = ActiveMessage.ActCzjjInfoResponse.newBuilder();
		
		ActiveModule_成长基金 am = ActiveModule_成长基金.get(pcode);
		if(am == null) {
			response.setBuytime(0);
			send(messageSource, response, Result.DataError);
			return;
		}
		
		response.setBuytime(am.getBuytime().getMillis());
		response.addAllGetid(am.getGetids());
		
		Map<String, actgradefund> agfs = ActDataCenter.getAllData(actgradefund.class, pcode);
		for(actgradefund agf : agfs.values()) {
			ActiveMessage.CzjjInfo.Builder build = ActiveMessage.CzjjInfo.newBuilder();
			build.setId(agf.getId());
			build.setCondition(agf.getDonum());
			
			Currency leftcur = Currency.create(agf.getRewardtype(), agf.getRewardid(), agf.getRewardnum());
			Currency rightcur = Currency.create(agf.getRewardtype2(), agf.getRewardid2(), agf.getRewardnum2());
			PackageMessage.Item leftitem = new CurrencyHandle().packItem(leftcur);
			PackageMessage.Item rightitem = new CurrencyHandle().packItem(rightcur);
			build.setLeftcur(leftitem);
			build.setRightcur(rightitem);
			
			response.addCzjjinfos(build.build());
		}

		actgradefund actgradefund = agfs.values().iterator().next();
		response.setRebate(actgradefund.getRebate()); //返利倍数
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActCzjjInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
